from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

# This model may be expando type sometime to allow users to add properties
class Sketch(db.Model):
    author = db.UserProperty()
    creation_date = db.DateTimeProperty(auto_now_add=True) #is this cached? auto_add_now?
    title = db.StringProperty()
    tag = db.StringProperty()
    text_content = db.TextProperty()

# todo: properly build the response
class CreateSketch(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type']= 'text/html'
        self.response.out.write("""
          <html>
            <body>
                <form action="/create" method="post">
                  Title: <br /><input type="text" name="title" maxlength="500" /><br /><br />
                  Tag: <br /><input type="text" name="tag" maxlength="500" /><br /><br />
                  Content: <br/><textarea name="text_content" rows="4" cols="40"></textarea><br /><br /></div>
                  <input type="submit" value="Sketch!" />
                </form>
            </body>
          </html>""")

    def post(self):
        sketch = Sketch()
        sketch.author = users.get_current_user()
        sketch.tag = self.request.get('tag')
        sketch.title = self.request.get('title')
        sketch.text_content = self.request.get('text_content')
        sketch.put()
        self.response.out.write('sketch entity defined and created redirecting')
        self.redirect("/view")
